
        package com.lmk;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {

    /** 图片存放的路径 */
    static String folder = "D:\\dev\\java三十六讲\\音频";

    /** 目标页面 */
    static String urlPath = "https://d.shikey.com/jike/%E5%B7%B2%E5%AE%8C%E7%BB%93%E7%9A%84%E8%AF%BE%E7%A8%8B/02%20java%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF36%E8%AE%B2/%E9%9F%B3%E9%A2%91/ ";

    public static void main(String[] args) {
        parsePage();
    }

    /**
     * 解析页面
     */
    static void parsePage(){
        // 1. 获得网页文档
        try {
            Document document = Jsoup.connect(urlPath).get();
            Elements imgs = document.select(".aMain .cont .mp3 mp3");

            imgs.forEach(img -> {
                String src = img.attr("src");
                download(src);
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void download(String src) {
        String filename = src.substring(0, src.indexOf("%"));
        filename = filename.substring(filename.lastIndexOf("F") + 1);
        System.out.println(filename);

        try {
            URL url = new URL(src);
            File file = new File(folder + filename);
            FileUtils.copyURLToFile(url, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
